package com.nxy.test.rxjava;

import java.util.concurrent.Executors;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.internal.schedulers.ExecutorScheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by snailnan on 2017/11/22.
 */
public class RxExample {
    private static final Logger logger = LoggerFactory.getLogger(RxExample.class);

    public static void main(String[] args) {
        Observable<Object> o = Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> e) throws Exception {
                logger.info("hello 开始 time:{}",System.currentTimeMillis());
                e.onNext("hello");
                e.onComplete();
                logger.info("hello 结束 time:{}",System.currentTimeMillis());
            }
        });
        Observable<Object> o2 = Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> e) throws Exception {
                logger.info("world 开始 time:{}",System.currentTimeMillis());
                Thread.sleep(3000);
                e.onNext("world");
                e.onComplete();
                logger.info("world 结束 time:{}",System.currentTimeMillis());
            }
        }).mergeWith(o).subscribeOn(new ExecutorScheduler(Executors.newFixedThreadPool(3)));
        MyObserver observer = new MyObserver();
//        o.subscribe(observer);
        o2.subscribe(observer);
        logger.info("hello world finsh");
    }

}
